//
//  RNetWorkRequestManager.m
//  EPAa
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 李日青. All rights reserved.
//

#import "RNetWorkRequestManager.h"

@interface RNetWorkRequestManager ()
// 存储回调block的属性
//@property (nonatomic,copy)ResultBlock block;

@end


@implementation RNetWorkRequestManager


+ (instancetype)defaultManager {
    static RNetWorkRequestManager *manager = nil;
    @synchronized(self) {
        if (!manager) {
            manager = [[RNetWorkRequestManager alloc]init];
        }
    }
    return manager;
}


//请求方法
+ (void)requestURL:(NSURL *)url Method:(URLRequestMethod)method Params:(NSDictionary *)param Result:(ResultBlock)resultBlock {
    //空的可变字符串
    NSMutableString *urlString = [NSMutableString new];
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接url
        [urlString appendString:url.absoluteString];
    }else if ([url isKindOfClass:[NSString class]]) {
        [urlString appendString:(NSString *)url];
    }
    if (urlString.length == 0) {
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reson":@"你输入的网址不正确"}];
        resultBlock (nil,nil,error);
        return;
        
    }
    //把参数字典转为字符串
    RNetWorkRequestManager *manager = [RNetWorkRequestManager new];
//    manager.block = resultBlock;
    NSString *paraStr = [manager strFromDict:param];
    switch (method) {
        case CK_GET:
        {
            if (paraStr.length > 0) {
                [urlString appendFormat:@"?%@",paraStr];
            }
            //当是GET请求的时候需要拼接一下网络连接地址
            [manager requestURL:[NSURL URLWithString:urlString] resultBlock:resultBlock];
        }
            break;
        case CK_POST:
            //当是POST的请求的时候参数是放在BODY里面的，不需要拼接网络连接
            [manager postURl:urlString paraStr:paraStr REsultBlock:resultBlock];
            break;
        default:
            break;
    }
    
    
}

//Get网络请求
- (void)requestURL:(NSURL *)url resultBlock:(ResultBlock)block {
//    __weak typeof (self) weakSelf = self;
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:block];
    //开始网络请求
    [dataTask resume];
}

//Post网络请求
- (void)postURl:(NSString *)urlStr paraStr:(NSString *)parstr REsultBlock:(ResultBlock)block {
    //创建NSURL
    NSURL *url = [NSURL URLWithString:urlStr];
    //创建可变请求
    NSMutableURLRequest *mReQuest = [NSMutableURLRequest requestWithURL:url];
    //设置POST
    [mReQuest setHTTPMethod:@"POST"];
    //求轻体
    [mReQuest setHTTPBody:[parstr dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mReQuest completionHandler:block];
    
    [dataTask resume];
}




- (NSString *)strFromDict:(NSDictionary *)dict {

    NSMutableArray *arrar = [NSMutableArray array];
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [arrar addObject:str];
    }
    //数组拼接字符串
    NSString *para = [arrar componentsJoinedByString:@"&"];
    NSLog(@"%@",para);
    //返回
    return para;
    
}




@end
